package com.yfp.operation.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.operation.domain.ClientCoupon;
import com.yfp.operation.domain.MarActivity;
import com.yfp.operation.domain.dto.ActivityDTO;
import com.yfp.operation.domain.dto.InviteRecordDTO;
import com.yfp.operation.domain.dto.ParticipationDTO;
import com.yfp.operation.domain.vo.ActivityVO;
import com.yfp.operation.domain.vo.InviteRecordVO;
import com.yfp.operation.domain.vo.ParticipationDetailVO;
import com.yfp.operation.service.MarActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 
 * @version 1.0
 * @date 2024-07-17 17:42
 */
@RestController
@RequestMapping("marActivity")
@Tag(name = "活动-pc")
public class MarActivityController extends BaseController {

    @Resource
    private MarActivityService marActivityService;


    @PostMapping("/add")
    @Operation(summary = "新增活动")
    public R<Void> add(@RequestBody MarActivity marActivity) {
        marActivityService.add(marActivity);
        return R.ok();
    }


    @GetMapping("/list")
    @Operation(summary = "营销活动列表")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = ActivityVO.class)
                    ))
            }
    )
    public R<TableDataInfo> activityList(ActivityDTO activityDTO) {
        startPage();
        List<ActivityVO> list = marActivityService.selectMarActivity(activityDTO);
        TableDataInfo dataTable = getDataTable(list);
        List<ActivityVO> activityVOS = marActivityService.complement(list);
        dataTable.setRows(activityVOS);
        return R.ok(dataTable);
    }


    @GetMapping("/invite")
    @Operation(summary = "邀请记录")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = InviteRecordVO.class)
                    ))
            }
    )
    public R<TableDataInfo> inviteRecord(InviteRecordDTO inviteRecordDTO) {
        startPage();
        List<InviteRecordVO> inviteRecordVOS = marActivityService.inviteRecord(inviteRecordDTO);
        TableDataInfo dataTable = getDataTable(inviteRecordVOS);
        return R.ok(dataTable);

    }


    /**
     * 参与详情
     *
     * @param participationDTO
     * @return
     */
    @GetMapping("/participation")
    @Operation(summary = "参与详情")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = ParticipationDetailVO.class)
                    ))
            }
    )
    public R<TableDataInfo> participation(ParticipationDTO participationDTO) {
        startPage();
        List<ParticipationDetailVO> list = marActivityService.participation(participationDTO);
        TableDataInfo dataTable = getDataTable(list);
        return R.ok(dataTable);
    }


    @GetMapping("/findActivityAward")
    @Operation(summary = "查看奖励")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = ClientCoupon.class)
                    ))
            }
    )
    public R<TableDataInfo> findActivityAward(ParticipationDTO participationDTO) {
        startPage();
        List<ClientCoupon> clientCoupons = marActivityService.findActivityAward(participationDTO);
        TableDataInfo dataTable = getDataTable(clientCoupons);
        return R.ok(dataTable);
    }


    /**
     * 根据id获取活动详情
     *
     * @return
     */
    @GetMapping("/id")
    @Operation(summary = "根据id获取活动详情")
    public R<MarActivity> getById(Long marActivityId) {
        return R.ok(marActivityService.selectById(marActivityId));
    }


    @PutMapping("/updateActivity")
    @Operation(summary = "修改活动")
    public R<Void> updateActivity(@RequestBody MarActivity marActivity) {
        marActivityService.updateActivity(marActivity);
        return R.ok();
    }


    @PutMapping("/efficacy")
    @Log(title = "pc-修改活动状态", businessType = BusinessType.UPDATE)
    @Operation(summary = "修改状态")
    public R<Void> updateStatus(@RequestParam("marActivityId") Long marActivityId, @RequestParam("status") @Schema(description = "状态0失效1正常2已过期3已删除4未开始") Integer status) {
        marActivityService.updateStatus(marActivityId, status);
        return R.ok();
    }

    @GetMapping("/issue/invite/coupon")
    @Operation(summary = "邀请有礼活动-发放邀请人优惠券")
    public R<Void> issueInviteCoupon(String passiveClientId) throws InterruptedException {
        marActivityService.issueInviteCoupon(passiveClientId);
        return R.ok();
    }
}
